#!/usr/bin/perl

use strict;

use LWP::Simple;
use Cwd qw(getcwd);
use File::Spec;

my $program = "ogg123 -q";
my $ogg_file1 = "/etc/alerter/ubuntu.ogg";
my $ogg_file2 = "/etc/alerter/horn.ogg";
my $user = "USER";
my $pass = "PASSWORD";
my $flag_to_temp = 0;
my $with_dc = "SECTIONS";


my $pid_file = "/etc/alerter/.scripte.pid";

defined(my $pid = fork) or die "Can't fork: $!";
exit if $pid;
umask 0;
open(PID," > $pid_file");
print PID $$;
close PID;


for(;;)
{

	my $pid_ogg = `ps -e | grep ogg | awk '{print \$4}'`;
	my $col_url = get "http://";
	$col_url =~ s/\\n/<br>/g;
	$col_url =~ s/<br>/\n/;

	my $temp_url = get "http://";
	$temp_url =~ s/\\n/<br>/g;
	$temp_url =~ s/<br>/\n/;
	

	my $reboot = get "http://";  
	my $alert = get "http://";
#	my $alert = "";

	if (length($pid_ogg) > 0){
		system "killall -9 $pid_ogg";
	}

	if(length($col_url) > 0){
		system "$program $ogg_file2";
	}

	if((length($reboot) > 0)){# || (length($alert) > 0)){
		system "$program $ogg_file1";
	}


	if((length($temp_url) > 0) && !$flag_to_temp){
		system "$program $ogg_file2";	
		++$flag_to_temp;
	}
	$flag_to_temp = 0 if length($temp_url) == 0;
	
	sleep(30);
}
